﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITAP.Core.Modelling;

namespace ITAP.ShprossesBuilder.Modelling.Components.Actions
{
    public class ActionAlignSingleShpross : IComponentAction
    {
        private ComponentAlign align;
        private bool moveConnectedComponents;

        public ActionAlignSingleShpross(ComponentAlign align, bool moveConnectedComponents)
        {
            this.align = align;
            this.moveConnectedComponents = moveConnectedComponents;
        }

        #region IComponentAction Members

        public bool Action(IComponent component)
        {
            if (!(component is Shpross))
                return false;

            Shpross shpross = component as Shpross;
            switch (align)
            {
                case ComponentAlign.Horizontal:
                    return this.HorizontalAlign(shpross);
                case ComponentAlign.Vertical:
                    return this.VerticalAlign(shpross);
                default:
                    return false;
            }
            return false;
        }

        #endregion

        private bool HorizontalAlign(Shpross shpross)
        {
            ModelPoint shprossSelectedPoint = shpross.GetSelectedPoint();
            ModelPoint moveToPoint;
            switch (shpross.SelectedPoint)
            {
                case ShprossPoints.Start:
                    moveToPoint = new ModelPoint(shpross.EndPoint.X, shprossSelectedPoint.Y);
                    if (moveConnectedComponents && shpross.EndConnectedComponent != null)
                    {
                        ModelVector vector = new ModelVector(shpross.EndConnectedComponent.Position, moveToPoint);
                        if (shpross.EndConnectedComponent.GetType() == typeof(Connector))
                            shpross.EndConnectedComponent.Parent.MoveWithOffset(vector);
                    }
                    shpross.MoveEndPointTo(moveToPoint);
                    return true;
                case ShprossPoints.End:
                    moveToPoint = new ModelPoint(shpross.StartPoint.X, shprossSelectedPoint.Y);
                    if (moveConnectedComponents && shpross.StartConnectedComponent != null)
                    {
                        ModelVector vector = new ModelVector(shpross.StartConnectedComponent.Position, moveToPoint);
                        if (shpross.StartConnectedComponent.GetType() == typeof(Connector))
                            shpross.StartConnectedComponent.Parent.MoveWithOffset(vector);
                    }
                    shpross.MoveStartPointTo(moveToPoint);
                    return true;
                default:
                    return false;
            }
            return false;
        }

        private bool VerticalAlign(Shpross shpross)
        {
            ModelPoint shprossSelectedPoint = shpross.GetSelectedPoint();
            ModelPoint moveToPoint;

            switch (shpross.SelectedPoint)
            {
                case ShprossPoints.Start:
                    moveToPoint = new ModelPoint(shprossSelectedPoint.X, shpross.EndPoint.Y);
                    if (moveConnectedComponents && shpross.EndConnectedComponent != null)
                    {
                        ModelVector vector = new ModelVector(shpross.EndConnectedComponent.Position, moveToPoint);
                        if (shpross.EndConnectedComponent.GetType() == typeof(Connector))
                            shpross.EndConnectedComponent.Parent.MoveWithOffset(vector);
                    }
                    shpross.MoveEndPointTo(moveToPoint);
                    return true;
                case ShprossPoints.End:
                    moveToPoint = new ModelPoint(shprossSelectedPoint.X, shpross.StartPoint.Y);
                    if (moveConnectedComponents && shpross.StartConnectedComponent != null)
                    {
                        ModelVector vector = new ModelVector(shpross.StartConnectedComponent.Position, moveToPoint);
                        if (shpross.StartConnectedComponent.GetType() == typeof(Connector))
                            shpross.StartConnectedComponent.Parent.MoveWithOffset(vector);
                    }

                    shpross.MoveStartPointTo(moveToPoint);
                    return true;
                default:
                    return false;
            }
            return false;
        }
    }
}
